package com.github.chirspan.xaas.uc.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.chirspan.xaas.uc.dao.UcRbacRoleDao;
import com.github.chirspan.xaas.uc.dao.UcRbacRoleResourceDao;
import com.github.chirspan.xaas.rbac.model.RbacRole;
import com.github.chirspan.xaas.rbac.model.RbacRoleResource;
import com.github.chirspan.xaas.rbac.service.impl.RoleServiceImpl;
import com.github.chirspan.xaas.uc.service.IRbacRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * ************************
 * <p>角色ServiceImpl</p>
 * *************************
 *
 * @author chenpan
 * @date 2021/2/8 11:50
 */
@Service
@Primary
public class RbacRoleServiceImpl extends RoleServiceImpl implements IRbacRoleService {

    @Autowired
    private UcRbacRoleDao roleDao;

    @Autowired
    private UcRbacRoleResourceDao roleResourceDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class})
    public void updateRoleResource(String roleId, String[] resourceIds) {
        LambdaQueryWrapper<RbacRoleResource> queryWrapper = Wrappers.lambdaQuery(RbacRoleResource.class);
        queryWrapper.eq(RbacRoleResource::getRoleId, roleId);
        roleResourceDao.delete(queryWrapper);
        if (resourceIds != null) {
            for (String resourceId : resourceIds) {
                RbacRoleResource roleResource = new RbacRoleResource();
                roleResource.setResourceId(resourceId);
                roleResource.setRoleId(roleId);
                roleResourceDao.insert(roleResource);
            }
        }
    }

    @Override
    public List<RbacRoleResource> getRoleResourcesById(String roleId) {
        LambdaQueryWrapper<RbacRoleResource> queryWrapper = Wrappers.lambdaQuery(RbacRoleResource.class);
        queryWrapper.eq(RbacRoleResource::getRoleId, roleId);
        return roleResourceDao.selectList(queryWrapper);
    }

    @Override
    @Cacheable(value = "rbac:roleSet", key = "T(String).join(#p0, '|')", unless = "#result == null || #result.size() == 0")
    public List<String> getRoleIdsByRole(Set<String> roles) {
        LambdaQueryWrapper<RbacRole> queryWrapper = Wrappers.lambdaQuery(RbacRole.class);
        queryWrapper.in(RbacRole::getRoleCode, roles);
        List<RbacRole> roleList = roleDao.selectList(queryWrapper);

        if (CollectionUtil.isNotEmpty(roleList)) {
            return roleList.stream().map(RbacRole::getId).collect(Collectors.toList());
        }
        return null;
    }
}
